﻿<!DOCTYPE>
<html>
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=EDGE;IE=11;IE=8,chrome=1" />
    <title>无限产能设备负荷统计</title>
    <link href="/InSiteWebApplication/Content/Easyui/themes/metro/easyui.css" rel="stylesheet" />
    <link href="/InSiteWebApplication/Content/Easyui/custom-easyui.css" rel="stylesheet" />
    <link href="/InSiteWebApplication/Content/Easyui/themes/icon.css" rel="stylesheet" />
</head>
<body>
    <div id="mainSec" style="margin: 0px 1px 0px 0px">
        <div class="panelSection">
            <div class="easyui-panel " title="查询区域" style="padding: 3px; width: 100%" data-options="closable:false,collapsible:true,minimizable:false,maximizable:false">
                <form id="queryForm" action="">
                <div style="margin: 3px 0px 3px 3px">
                    <div style="float: left;">
                        <div class="divLabel" style="margin-bottom: 5px">
                            工区：</div>
                        <select style='width: 120px; height: 20px' id='ddlWorkCenter' name="WorkCenterName"
                            data-options='editable:false,panelHeight:200' class='easyui-combobox'>
                        </select>
                    </div>
                    <div style="float: left; margin-left: 10px">
                        <div class="divLabel" style="margin-bottom: 5px">
                            班组：</div>
                        <select style='width: 120px; height: 20px' id='ddlTeam' name="TeamName" data-options='editable:false,panelHeight:200'
                            class='easyui-combobox'>
                        </select>
                    </div>
                    <div style="float: left; margin-left: 10px">
                        <div class="divLabel" style="margin-bottom: 5px">
                            设备编号：</div>
                        <input id="txtResourceName" name="ResourceName" class="easyui-textbox" style="width: 120px;
                            height: 20px" />
                    </div>
                    <div style="float: left; margin-left: 10px">
                        <div class="divLabel" style="margin-bottom: 5px">
                            日期范围：</div>
                        <input id="txtStartDate" name="StartDate" class="easyui-datebox" style="width: 120px;
                            height: 20px" />-
                        <input id="txtEndDate" name="EndDate" class="easyui-datebox" style="width: 120px;
                            height: 20px" />
                    </div>
                    <div style="float: left; margin-left: 10px">
                        <button id="btnSearch" class="easyui-linkbutton" type="button" data-options="" style="margin-top: 16px;
                            width: 80px;">
                            查询</button>
                        <button id="btnReset" class="easyui-linkbutton" type="button" data-options="" style="margin-top: 16px;
                            width: 50px;">
                            重置</button>
                    </div>
                    <div style="clear: both">
                    </div>
                </div>
                </form>
            </div>
        </div>
        <div class="panelSection">
            <div class="easyui-panel " title="设备负荷信息" style="padding: 3px; width: 100%" data-options="closable:false,collapsible:true,minimizable:false,maximizable:false">
                <div id="dvApsPlanInfo" class="dvgrid-cellwarp" style="background: #fff; margin: 3px 0px 3px 3px">
                    <button id="btnExportTotal" class="easyui-linkbutton" type="button" data-options=""
                        style="width: 120px;">
                        导出负荷</button>
                    <div style="width: 99%">
                        <table id="gdResourceInfo" class="easyui-datagrid" style="width: 100%; height: 500px;"
                            data-options="fitColumns:true,rownumbers:true,singleSelect:true">
                        </table>
                    </div>
                    <div style="clear: both">
                    </div>
                </div>
            </div>
        </div>
        <div class="panelSection">
            <div class="easyui-panel " title="设备负荷明细" style="padding: 3px; width: 100%" data-options="closable:false,collapsible:true,minimizable:false,maximizable:false">
                <div class="dvgrid-cellwarp" style="background: #fff; margin: 3px 0px 3px 3px">
                    <div id="mainView" style="height: 300px">
                    </div>
                    <div style="clear: both">
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div id="dd" style="display: none;">
        <div style="padding: 3px 0px 0px 3px">
            <button id="btnDownload" class="easyui-linkbutton" type="button" data-options=""
                style="width: 80px;">
                下载</button>
            <table id="dgResApsDetailInfo" class="easyui-datagrid" style="width: 99%; height: 600px;"
                data-options="rownumbers:true">
            </table>
        </div>
    </div>
</body>
</html>
<script src="/InSiteWebApplication/Scripts/echarts.min.js" type="text/javascript"></script>
<script src="/InSiteWebApplication/Scripts/CustomPageJs/ReferenceJsLib.js" type="text/javascript"></script>
<script src="/InSiteWebApplication/Content/Easyui/jquery.min.js" type="text/javascript"></script>
<script src="/InSiteWebApplication/Content/Easyui/jquery.easyui.min.js" type="text/javascript"></script>
<script src="/InSiteWebApplication/Content/Easyui/easyui-lang-zh_CN.js" type="text/javascript"></script>
<script src="/InSiteWebApplication/Scripts/CustomPageJs/CustomCommon.js" type="text/javascript"></script>
<script type="text/javascript">
    /*
    --创建人：zhangrj
    --日期：2025-08-04
    --描述：无限产能设备负荷统计JS
    */
    var page = {
        rootPath: getRootPath(), //网站根路径
        url: getRootPath() + "/Ashx/ApsHandler/ApsInfiniteCapacity.ashx", //后台处理逻辑URL
        commonUrl: getRootPath() + "/Ashx/CommonHandler.ashx", //通用URL
        //初始化页面
        init: function () {
            page.bind();
            resDetailView.bind();
            page.initPageData();
        },
        //页面事件绑定
        bind: function () {
            //绑定数据和列
            $("#gdResourceInfo").datagrid({
                columns: [[
                { field: 'RESOURCENAME', title: '设备编号', width: 150 },
                { field: 'RESOURCEDESC', title: '设备名称', width: 150 },
                { field: 'WORKCENTERNAME', title: '工区', width: 120 },
                { field: 'TEAMNAME', title: '班组', width: 120 },
                { field: 'WORKSHIFTNAME', title: '班次', width: 180 },
                { field: 'WORKSHIFTHOURS', title: '班次时长', width: 80 },
                { field: 'MAKECALENDARNAME', title: '日历', width: 100 },
                { field: 'TOTALLOAD', title: '设备总负荷', width: 80, formatter: function (value, row, index) {
                    return value + "%";
                }
                }
                ]],
                onClickRow: function (rowIndex, rowData) {
                    page.bindResApsData(rowData);
                },
                rowStyler: function (index, row) {
                    if (row.设备总负荷 > 0) {
                        return 'background-color:red;';
                    }
                }
            });
            //查询按钮
            $('#btnSearch').on("click", function () {
                page.clearPage(false, true);
                page.searchData();
            });
            //清除按钮
            $('#btnReset').on("click", function () {
                page.clearPage(true, true);
            });
            //导出总的负荷
            $("#btnExportTotal").on("click", function () {
                var rows = $("#gdResourceInfo").datagrid("getRows");
                if (rows.length === 0) {
                    displayMessage("请查询数据", false);
                    return;
                }
                var lastData = [];
                for (var i = 0; i < rows.length; i++) {
                    lastData.push({
                        设备编号: rows[i].RESOURCENAME,
                        设备名称: rows[i].RESOURCEDESC,
                        工区: rows[i].WORKCENTERNAME,
                        班组: rows[i].TEAMNAME,
                        班次: rows[i].WORKSHIFTNAME,
                        班次时长: rows[i].WORKSHIFTHOURS,
                        日历: rows[i].MAKECALENDARNAME,
                        设备总负荷: rows[i].TOTALLOAD
                    });
                }
                FileDownLoad(page.commonUrl, { action: "DownloadExcelByRowsData", rowDatas: JSON.stringify(lastData), fileName: "设备总负荷记录表" });
            });
        },
        //初始化页面数据
        initPageData: function () {
            //绑定查询的工区
            $('#ddlWorkCenter').combobox({
                url: page.commonUrl + "?action=getWorkcenterByFactory",
                valueField: 'WORKCENTERID',
                textField: 'WORKCENTERNAME',
                onSelect: function (rec) {
                    page.bindTeamData(rec.WORKCENTERID);
                }
            });
        },
        //绑定班组（查询）
        bindTeamData: function (workcenterID) {
            page.post(page.commonUrl, { action: "GetTeam", workCenterId: workcenterID }, true, function (res) {
                if (!res.Result) {
                    displayMessage(res.Message, res.Result);
                    return;
                }
                $('#ddlTeam').combobox({
                    valueField: 'TEAMID',
                    textField: 'TEAMNAME',
                    data: res.DBTable
                });
            });
        },
        //查询
        searchData: function () {
            var params = {};
            var queryFormData = $("#queryForm").serializeArray();
            $.each(queryFormData, function () {
                params[this.name] = this.value;
            });
            page.post(page.url, { action: "GetAllApsResourceLoadData", params: JSON.stringify(params) }, true, function (res) {
                if (!res.Result) {
                    displayMessage(res.Message, res.Result);
                    return;
                }
                $("#gdResourceInfo").datagrid({ data: res.DBTable }); //绑定数据
            });
        },
        //绑定设备下的任务信息
        bindResApsData: function (rowData) {
            resDetailView.init(); //初始化
            page.post(page.url, { action: "GetResourceLoadDetail", resourceID: rowData.RESOURCEID }, true, function (res) {
                if (res.Result) {
                    resDetailView.resourceID = rowData.RESOURCEID;
                    for (var i = 0; i < res.ReturnData.length; i++) {
                        resDetailView.xData.push(res.ReturnData[i].DayDate);
                        resDetailView.yData.push(res.ReturnData[i].LoadValue);
                        if (res.ReturnData[i].LoadValue > resDetailView.maxYData) {
                            resDetailView.maxYData = res.ReturnData[i].LoadValue
                        }
                    }
                    resDetailView.viewBar(); //绘制柱状图
                }
                displayMessage(res.Message, res.Result);
            });
        },
        //禁用页面按钮
        disableBtn: function () {
            $("#mainSec").find(".easyui-linkbutton").each(function () {
                $(this).linkbutton('disable');
            });
        },
        //启用页面按钮
        enableBtn: function () {
            $("#mainSec").find(".easyui-linkbutton").each(function () {
                $(this).linkbutton('enable');
            });
        },
        //清除页面（查询区域，列表区域）
        clearPage: function (query, list) {
            if (query) {
                $("#ddlWorkCenter").combobox("clear"); //工区
                $("#ddlTeam").combobox("clear"); //班组
                $("#txtResourceName").textbox("clear"); //设备
                $("#txtStartDate").datebox("clear"); //开始日期
                $("#txtEndDate").datebox("clear"); //结束日期
            }
            if (list) {
                clearEasyuiDataGrid("gdResourceInfo"); //清空列表
                resDetailView.init(); //初始化柱状图
            }
        },
        //post数据（路径，参数，是否异步，成功返回方法）
        post: function (url, param, async, callback) {
            $.ajax({
                type: "Post",
                url: url,
                data: param,
                cashe: false,
                async: async,
                dataType: "json",
                beforeSend: function () {
                    $.messager.progress({ text: '', msg: '处理中...' });
                },
                success: function (data) {
                    $.messager.progress('close');
                    callback(data);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    $.messager.progress('close');
                    displayMessage(XMLHttpRequest.responseText, false);
                    $("body").find(".easyui-linkbutton").each(function () {
                        $(this).linkbutton('enable');
                    });
                }
            });
        }
    };
    /*设备明细柱状图绘制*/
    var resDetailView = {
        myChart: null, //图表对象
        resourceID: "", //对应的设备ID
        xData: [], //X轴数据
        yData: [], //Y轴数据
        maxYData: 100, //Y轴最大值
        //绑定事件
        bind: function () {
            var dom = document.getElementById('mainView');
            this.myChart = echarts.init(dom);
            this.myChart.on('click', function (params) {
                if (params.componentType === 'series') {
                    // 确保是柱状图被点击
                    if (params.seriesType === 'bar') {
                        resDetailView.bindApsDetail(params.name);
                    }
                }
            });
            $("#dgResApsDetailInfo").datagrid({
                columns: [[
                { field: 'MfgOrderName', title: '订单号', width: 120 },
                { field: 'ContainerName', title: '批次号', width: 150 },
                { field: 'WorkflowInfo', title: '工艺信息', width: 180 },
                { field: 'WorkFlowStepName', title: '工序', width: 150 },
                {
                    field: 'StartTime', title: '排产开始时间', width: 150, formatter: function (value, row, index) {
                        if (!isNullOrWhiteSpaceStr(value)) {
                            return dayjs(value).format('YYYY-MM-DD HH:mm:ss');
                        }
                    }
                },
                {
                    field: 'EndTime', title: '排产结束时间', width: 150, formatter: function (value, row, index) {
                        if (!isNullOrWhiteSpaceStr(value)) {
                            return dayjs(value).format('YYYY-MM-DD HH:mm:ss');
                        }
                    }
                },
                { field: 'ResPercentage', title: '设备负荷占比', width: 100 },
                { field: 'ResWorkTimeHours', title: '设备加工时长', width: 100 }
                ]]
            });
            //下载按钮
            $('#btnDownload').on("click", function () {
                var rows = $("#dgResApsDetailInfo").datagrid("getRows");
                if(rows.length === 0)
                {
                    displayMessage("暂无数据可导出", false);
                    return;
                }
                var lastData = [];
                for (var i = 0; i < rows.length; i++) {
                    lastData.push({
                        订单号: rows[i].MfgOrderName,
                        批次号: rows[i].ContainerName,
                        工艺信息: rows[i].WorkflowInfo,
                        工序: rows[i].WorkFlowStepName,
                        排产开始时间: rows[i].StartTime,
                        排产结束时间: rows[i].EndTime,
                        设备负荷占比: rows[i].ResPercentage,
                        设备加工时长: rows[i].ResWorkTimeHours
                    });
                }
                FileDownLoad(page.commonUrl, { action: "DownloadExcelByRowsData", rowDatas: JSON.stringify(lastData), fileName: "设备日期排产明细记录表" })
            });
        },
        //初始化
        init: function () {
            this.xData = [];
            this.yData = [];
            this.maxYData = 100;
            this.resourceID = "";
            this.viewBar();
            clearEasyuiDataGrid("dgResApsDetailInfo"); //清空列表
        },
        //绘制柱状图
        viewBar: function () {
            var option = {
                color: ['#00DDFF'],
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'cross',
                        label: {
                            backgroundColor: '#6a7985'
                        }
                    },
                    formatter: function (params) {
                        return params[0].name + '<br/>' + params[0].seriesName + ' : ' + params[0].value + "%";
                    }
                },
                grid: {
                    top: '6%',
                    right: '3%',
                    left: '0%',
                    bottom: '0%',
                    containLabel: true
                },
                xAxis: [{
                    type: 'category',
                    data: resDetailView.xData
                }],
                yAxis: [{
                    type: 'value',
                    scale: true,
                    max: resDetailView.maxYData,
                    min: 0
                }],
                series: [{
                    name: '设备负荷',
                    type: 'bar',
                    data: resDetailView.yData,
                    label: {
                        show: true,
                        position: 'inside',
                        formatter: (params) => params.value + '%'
                    }
                }]
            };
            this.myChart.setOption(option);
            window.addEventListener('resize', this.myChart.resize);
        },
        //绑定排产明细
        bindApsDetail: function (cDate) {
            var params = {
                action: "GetResourceBarApsDetail",
                resourceID: resDetailView.resourceID,
                currDate: cDate
            };
            page.post(page.url, params, true, function (res) {
                if (!res.Result) {
                    displayMessage(res.Message, res.Result);
                    return;
                }
                $('#dd').dialog({
                    title: "排产明细",
                    width: 1000,
                    modal: true
                });
                $("#dgResApsDetailInfo").datagrid({ data: res.ReturnData }); //绑定数据
            });
        }
    };
    /*页面入口*/
    $(function () {
        page.init();
    });
</script>
